/**
 * com.yhte.mir.util
 * ConnectionUtil.java
 * sunsu
 * 2012-9-17
 */
package com.yhte.mir.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;

/**
 * @author sunsu
 *
 */
public class ConnectionUtil {
	private static Logger log = Logger.getLogger(ConnectionUtil.class.getName());
	private static Properties p = new Properties();
	private static Map<String,ConnInfo> map = new HashMap();
	private static Map<String,Connection> connMap = new HashMap();
	static{
		log.debug("加载配置文件");
		try {
			String configfile = "./conf/config.properties";
			p.load(new FileInputStream(configfile));
			ConnInfo ill = new ConnInfo();
			String driverClass1 = p.getProperty("ill_driverClass");
			if(driverClass1!= null && !"".equals(driverClass1)){
				ill.setDriverClass(driverClass1);
			}
			ill.setUrl(p.getProperty("ill_url"));
			ill.setUsername(p.getProperty("ill_username"));
			ill.setPwd(p.getProperty("ill_pwd"));
			map.put("ill", ill);
			
			ConnInfo center = new ConnInfo();
			String driverClass2 = p.getProperty("center_driverClass");
			if(driverClass2!= null && !"".equals(driverClass2)){
				center.setDriverClass(driverClass2);
			}
			center.setUrl(p.getProperty("center_url"));
			center.setUsername(p.getProperty("center_username"));
			center.setPwd(p.getProperty("center_pwd"));
			map.put("ceneter", center);
		} catch (IOException e) {
			log.error("加载配置文件出错："+e.getMessage());
		}
	}
	public static void initConnection() {
		Set keys = map.keySet();
		Iterator<String> it = keys.iterator();
		while(it.hasNext()){
			String key = it.next();
			ConnInfo c = map.get(key);
			Connection conn = getConnection(c);
			while(conn == null){
				log.info("数据库链接失败，重新链接:"+c.getUrl());
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {}
				conn = getConnection(map.get(it.next()));
			}
			connMap.put(key, conn);
		}
	}
	
	private static Connection getConnection(ConnInfo info){
		Connection conn = null;
		try {
			Class.forName(info.getDriverClass());
			conn = DriverManager.getConnection(info.getUrl(), info.getUsername(), info.getPwd());
		} catch (Exception e) {
			log.error("创建数据库链接失败："+e.getMessage());
		}finally{
			return conn;
		}
	}
	
	private static void closeConnection(){
		Set keys = connMap.keySet();
		Iterator<String> it = keys.iterator();
		while(it.hasNext()){
			Connection conn = connMap.get(it.next());
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					log.error("关闭数据库链接失败："+e.getMessage());
				}
			}
		}
	}
	
	public static void main(String[] args) {
		ConnectionUtil cu = new ConnectionUtil();
		cu.initConnection();
		cu.closeConnection();
	}
}
